package org.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.dto.DishDto;
import org.example.dto.SetmealDto;
import org.example.entity.Category;
import org.example.entity.Employee;
import org.example.entity.Setmeal;
import org.example.entity.SetmealDish;
import org.example.service.CategoryService;
import org.example.service.SetmealDishService;
import org.example.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody  SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }
    @GetMapping("/page")
    public R<Page> page (int page, int pageSize ,String name)
    {
        Page<Setmeal> pageinfo = new Page<>(page,pageSize);
        Page<SetmealDto>  dtoPage =new Page<>();
        LambdaQueryWrapper<Setmeal>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageinfo,queryWrapper);

        BeanUtils.copyProperties(pageinfo,dtoPage,"records");

        List<Setmeal> records = pageinfo.getRecords();

        List<SetmealDto>  List =  records.stream().map((item)->{
            SetmealDto setmealDto =new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null)
            {
                String categoryname = category.getName();
                setmealDto.setCategoryName(categoryname);
            }
            return  setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(List);
        return  R.success(dtoPage);
    }

    @DeleteMapping
    public  R<String>  delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        setmealService.removeWithDish(ids);

        return  R.success("套餐数据删除成功");
    }

    @PostMapping("/status/{status}")
    public R<String> update(@PathVariable int status,@RequestParam Long ids)
    {
//        log.info(setmealServicealDto.toString());

        log.info("ids is {}",ids);
        log.info("status is  {}", status);

        Setmeal setmeal = setmealService.getById(ids);
        setmealService.removeById(setmeal);
        log.info("setmeal is {}",setmeal);
        setmeal.setStatus(status);
        setmealService.save(setmeal);
//        QueryWrapper<Setmeal> queryWrapper =new QueryWrapper<>();
//        QueryWrapper<Setmeal> eq = queryWrapper.eq(Setmeal::getId, ids);
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getId, ids);
//        dishService.updateWithFlavor(dishDto);
        return  R.success("修改套餐状态成功");
    }
    @GetMapping("/list")
    public  R<List<Setmeal>> list(Setmeal setmeal)
    {
        LambdaQueryWrapper<Setmeal> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal>  list =setmealService.list(queryWrapper);
        return  R.success(list) ;
    }
}
